package com.controller;

import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import com.beans.Code;
import com.beans.Roller;
import com.service.SysManager;
import com.vo.Page;

public class RollerController extends UniversalController {
	private SysManager sysManager;

	public SysManager getSysManager() {
		return sysManager;
	}

	public void setSysManager(SysManager sysManager) {
		this.sysManager = sysManager;
	}


	public ModelAndView getRollerList(HttpServletRequest request,
			HttpServletResponse response) throws IllegalAccessException,
			InvocationTargetException {
		Map map = new HashMap();
		List list = null;
		list = this.sysManager.getRollerDao().findAll(Roller.class);
		map.put("o", list);
		return new ModelAndView("admin/rollerList", map);
	}

	public ModelAndView getRollerEdit(HttpServletRequest request,
			HttpServletResponse response) throws IllegalAccessException,
			InvocationTargetException {
		Map map = new HashMap();
		String id = request.getParameter("id");
		Roller m = null;
		if (null==id||"0".equals(id) || "-1".equals(id))
			m = new Roller();
		else
			m = (Roller) this.sysManager.getRollerDao().findByID(Roller.class,
					(Serializable) id);
		map.put("o", m);
		return new ModelAndView("admin/rollerEdit", map);
	}

	public ModelAndView getRollerFrame(HttpServletRequest request,
			HttpServletResponse response) throws IllegalAccessException,
			InvocationTargetException {
		return new ModelAndView("admin/rollerFrame");
	}
	public ModelAndView getSaveRoller(HttpServletRequest request,HttpServletResponse response){
		Roller m=new Roller();
		String msg="";
		try {
			bind(request,m);
			Roller m2=(Roller)getUniversalManager().findByID(Roller.class, m.getCode());
			if(m.getpCode()==null||"".equals(m.getpCode()))m.setpCode("0");
			if(m2==null)
				getUniversalManager().insert(m);
			else
				getUniversalManager().update(m);
			msg="操作成功！";
		} catch (Exception e) {
			e.printStackTrace();
			msg="操作失败！";
		}
		getMessage(response, msg);
		return null;
	}
	public ModelAndView getDeletRoller(HttpServletRequest request,HttpServletResponse response){
		String msg="";
		try {
			String code=request.getParameter("code");
			Roller m=(Roller)getUniversalManager().findByID(Roller.class, code);
			if(m!=null)
				getUniversalManager().delete(m);
			msg="操作成功！";
		} catch (Exception e) {
			e.printStackTrace();
			msg="操作失败！";
		}
		getMessage(response, msg);
		return null;
	}
	public ModelAndView getSelect(HttpServletRequest request,
			HttpServletResponse response) {
		String type = request.getParameter("type");
		String index = request.getParameter("index");
		String code = request.getParameter("code");
		Map map = new HashMap();
		List list = null;
			Page page = getUniversalManager().findPageByCriteria(1, 1000, map,Roller.class);
			if (page == null || page.getDataList() == null)
				return null;
			list = page.getDataList();
		String option = "";
		if (code == null || "".equals(code))
			for (Roller c : (List<Roller>) list) {
				if (c.getCode().equals(index))
					option += "<option value='" + c.getCode() + "' selected>"
							+ c.getName() + "</option>";
				else
					option += "<option value='" + c.getCode() + "'>"
							+ c.getName() + "</option>";
			}
		else
			for (Roller c : (List<Roller>) list) {
				if (c.getCode().equals(index))
					option += "<option value='" + c.getCode() + "' selected>["
							+ c.getCode() + "] " + c.getName() + "</option>";
				else
					option += "<option value='" + c.getCode() + "'>["
							+ c.getCode() + "] " + c.getName() + "</option>";
			}
		getMessage(response, option);
		return null;
	}
}